/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "geofence_srv_proxy.h"

#include <ipc_types.h>
#include <message_parcel.h>
#include <message_option.h>

#include "igeofence_callback.h"
#include "geofence_common.h"

namespace OHOS {
namespace Msdp {
void GeofenceSrvProxy::SubscribePoint(const GeofenceDataUtils::GeofenceType& type,\
    const GeofenceDataUtils::PointOptions& pointOptions, const sptr<IgeofenceCallback>& callback)
{
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    GEOFENCE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GeofenceSrvProxy::GetDescriptor())) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Write descriptor failed");
        return;
    }

    GEOFENCE_WRITE_PARCEL_NO_RET(data, Int32, type);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
    GEOFENCE_WRITE_PARCEL_NO_RET(data, DoubleVector, pointOptions.centerCoordinate);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, Double, pointOptions.radius);

    int ret = remote->SendRequest(static_cast<int>(Igeofence::GEOFENCE_SUBSCRIBEPOINT), data, reply, option);
    if (ret != ERR_OK) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Exit");
}

void GeofenceSrvProxy::UnSubscribePoint(const GeofenceDataUtils::GeofenceType& type,\
    const GeofenceDataUtils::PointOptions& pointOptions, const sptr<IgeofenceCallback>& callback)
{
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    GEOFENCE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GeofenceSrvProxy::GetDescriptor())) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Write descriptor failed!");
        return;
    }

    GEOFENCE_WRITE_PARCEL_NO_RET(data, Int32, type);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
    GEOFENCE_WRITE_PARCEL_NO_RET(data, DoubleVector, pointOptions.centerCoordinate);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, Double, pointOptions.radius);

    int ret = remote->SendRequest(static_cast<int>(Igeofence::GEOFENCE_UNSUBSCRIBEPOINT), data, reply, option);
    if (ret != ERR_OK) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Exit");
}

void GeofenceSrvProxy::SubscribePoi(const GeofenceDataUtils::GeofenceType& type,\
    const GeofenceDataUtils::PoiOptions& poiOptions, const sptr<IgeofenceCallback>& callback)
{
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    GEOFENCE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GeofenceSrvProxy::GetDescriptor())) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Write descriptor failed");
        return;
    }

    GEOFENCE_WRITE_PARCEL_NO_RET(data, Int32, type);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.keyword);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.poiType);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.poiCity);

    int ret = remote->SendRequest(static_cast<int>(Igeofence::GEOFENCE_SUBSCRIBEPOI), data, reply, option);
    if (ret != ERR_OK) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Exit");
}

void GeofenceSrvProxy::UnSubscribePoi(const GeofenceDataUtils::GeofenceType& type, \
    const GeofenceDataUtils::PoiOptions& poiOptions, const sptr<IgeofenceCallback>& callback)
{
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    GEOFENCE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GeofenceSrvProxy::GetDescriptor())) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Write descriptor failed!");
        return;
    }

    GEOFENCE_WRITE_PARCEL_NO_RET(data, Int32, type);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.keyword);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.poiType);
    GEOFENCE_WRITE_PARCEL_NO_RET(data, String, poiOptions.poiCity);

    int ret = remote->SendRequest(static_cast<int>(Igeofence::GEOFENCE_UNSUBSCRIBEPOI), data, reply, option);
    if (ret != ERR_OK) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Exit");
}

bool GeofenceSrvProxy::SetFoolproofTactics(const GeofenceDataUtils::GeofenceType& type, double minuteTime)
{
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    GEOFENCE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GeofenceSrvProxy::GetDescriptor())) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Write descriptor failed");
        return result;
    }

    GEOFENCE_WRITE_PARCEL_WITH_RET(data, Int32, type, false);
    GEOFENCE_WRITE_PARCEL_WITH_RET(data, Double, minuteTime, false);

    int ret = remote->SendRequest(static_cast<int>(Igeofence::GEOFENCE_SET_FOOLPROOF_TACTICS), data, reply, option);
    if (ret != ERR_OK) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return result;
    }
    if (!reply.ReadBool(result)) {
        GEOFENCE_HILOGE(GEOFENCE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }
    GEOFENCE_HILOGD(GEOFENCE_MODULE_INNERKIT, "Exit");
    return result;

}
} // Msdp
} // OHOS
